<template>
  <view class="container">
    <!-- 选项卡 -->
    <view class="tabs">
      <view 
        class="tab-item" 
        v-for="(tab, index) in tabs" 
        :key="index"
        :class="{active: currentTab === index}"
        @click="switchTab(index)"
      >
        <text>{{ tab }}</text>
      </view>
    </view>
    
    <!-- 优惠券列表 -->
    <scroll-view 
      scroll-y 
      class="coupon-list"
      refresher-enabled
      :refresher-triggered="refreshing"
      @refresherrefresh="onRefresh"
    >
      <view v-if="filteredCoupons.length > 0">
        <view 
          class="coupon-item" 
          v-for="coupon in filteredCoupons" 
          :key="coupon.id"
          :class="{disabled: coupon.status !== 1}"
        >
          <view class="coupon-left">
            <text class="coupon-amount" v-if="coupon.type === 1">¥{{ coupon.value }}</text>
            <text class="coupon-amount" v-else-if="coupon.type === 2">{{ (coupon.value * 10).toFixed(1) }}折</text>
            <text class="coupon-condition">满{{ coupon.minConsume }}可用</text>
          </view>
          <view class="coupon-right">
            <text class="coupon-name">{{ coupon.name }}</text>
            <text class="coupon-time">{{ coupon.useStartTime }} - {{ coupon.useEndTime }}</text>
            <text class="coupon-desc">{{ coupon.description }}</text>
            <view class="coupon-status" v-if="coupon.status === 0">
              <text>未生效</text>
            </view>
            <view class="coupon-status" v-if="coupon.status === 2">
              <text>已使用</text>
            </view>
            <view class="coupon-status" v-if="coupon.status === 3">
              <text>已过期</text>
            </view>
          </view>
        </view>
      </view>
      
      <!-- 空状态 -->
      <view v-else class="empty-state">
        <image class="empty-icon" src="/static/icons/empty-coupon.svg" mode="aspectFit"></image>
        <text class="empty-text">暂无优惠券</text>
        <button class="go-shopping-btn" @click="goShopping">去逛逛</button>
      </view>
    </scroll-view>
  </view>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { getCouponList } from '@/api/coupon.js'

const tabs = ['未生效', '可使用', '已使用', '已过期']
const currentTab = ref(0)
const coupons = ref([])
const refreshing = ref(false)

const filteredCoupons = computed(() => {
  const statusMap = {
    0: 0, // 未生效
    1: 1, // 可使用
    2: 2, // 已使用
    3: 3  // 已过期
  }
  return coupons.value.filter(coupon => coupon.status === statusMap[currentTab.value])
})

const switchTab = (index) => {
  currentTab.value = index
}

const loadCoupons = () => {
  // 模拟加载数据
  setTimeout(async () => {
    // 获取用户优惠券列表
    try {
      const res = await getCouponList()
      coupons.value = res.data
    } catch (error) {
      console.error('获取用户优惠券失败:', error)
      coupons.value = []
    }
  }, 500)
}

const onRefresh = () => {
  refreshing.value = true
  
  // 重新加载数据
  setTimeout(() => {
    loadCoupons()
    refreshing.value = false
  }, 1000)
}

const goShopping = () => {
  uni.switchTab({
    url: '/pages/index/index'
  })
}

onMounted(() => {
  loadCoupons()
})
</script>

<style lang="scss" scoped>
.container {
  min-height: 100vh;
  background-image: linear-gradient(135deg, #fff8f8 0%, #fff5f7 50%, #fff8fa 100%);
  display: flex;
  flex-direction: column;
}

.tabs {
  display: flex;
  background: rgba(255, 255, 255, 0.95);
  padding: 20rpx 30rpx;
  position: sticky;
  top: 0;
  z-index: 10;
  backdrop-filter: blur(20rpx);
  box-shadow: 0 4rpx 24rpx rgba(0, 0, 0, 0.06);
  
  .tab-item {
    flex: 1;
    text-align: center;
    font-size: 28rpx;
    color: #666;
    position: relative;
    padding: 16rpx 0;
    transition: all 0.3s ease;
    
    &.active {
      color: #ff6b6b;
      font-weight: 600;
      
      &::after {
        content: '';
        position: absolute;
        bottom: 0;
        left: 50%;
        transform: translateX(-50%);
        width: 40rpx;
        height: 4rpx;
        background: linear-gradient(to right, #ff9a9e, #ff6b6b);
        border-radius: 4rpx;
      }
    }
  }
}

.coupon-list {
  flex: 1;
  padding: 30rpx;
}

.coupon-item {
  display: flex;
  background: #fff;
  border-radius: 24rpx;
  margin-bottom: 30rpx;
  overflow: hidden;
  position: relative;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.03);
  transition: all 0.3s ease;
  
  &:active {
    transform: scale(0.98);
  }
  
  &.disabled {
    opacity: 0.7;
    filter: grayscale(0.5);
  }
  
  &::before {
    content: '';
    position: absolute;
    left: 200rpx;
    top: -10rpx;
    bottom: -10rpx;
    width: 20rpx;
    background-image: radial-gradient(
      circle at 0 50%,
      transparent 10rpx,
      #fff 10rpx
    );
    background-size: 20rpx 40rpx;
    background-repeat: repeat-y;
    z-index: 1;
  }
  
  .coupon-left {
    width: 220rpx;
    background: linear-gradient(135deg, #ff9a9e 0%, #ff6b6b 100%);
    color: #fff;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 40rpx 20rpx;
    position: relative;
    
    &::before {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background: linear-gradient(45deg,
        rgba(255, 255, 255, 0.1) 25%,
        transparent 25%,
        transparent 50%,
        rgba(255, 255, 255, 0.1) 50%,
        rgba(255, 255, 255, 0.1) 75%,
        transparent 75%,
        transparent
      );
      background-size: 80rpx 80rpx;
      opacity: 0.1;
    }
    
    .coupon-amount {
      font-size: 56rpx;
      font-weight: 600;
      margin-bottom: 12rpx;
      text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.1);
      
      &::before {
        font-size: 32rpx;
        margin-right: 4rpx;
      }
    }
    
    .coupon-condition {
      font-size: 24rpx;
      opacity: 0.9;
      background: rgba(255, 255, 255, 0.2);
      padding: 4rpx 16rpx;
      border-radius: 20rpx;
    }
  }
  
  .coupon-right {
    flex: 1;
    padding: 30rpx;
    position: relative;
    
    .coupon-name {
      font-size: 32rpx;
      font-weight: 600;
      margin-bottom: 12rpx;
      color: #333;
    }
    
    .coupon-time {
      font-size: 24rpx;
      color: #999;
      margin-bottom: 12rpx;
      display: flex;
      align-items: center;
      
      &::before {
        content: '🕒';
        margin-right: 8rpx;
        font-size: 24rpx;
      }
    }
    
    .coupon-desc {
      font-size: 26rpx;
      color: #666;
      line-height: 1.5;
    }
    
    .coupon-status {
      position: absolute;
      right: 30rpx;
      top: 30rpx;
      font-size: 24rpx;
      padding: 4rpx 16rpx;
      border-radius: 20rpx;
      background: rgba(153, 153, 153, 0.1);
      color: #999;
    }
  }
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 100rpx 0;
  
  .empty-icon {
    width: 240rpx;
    height: 240rpx;
    margin-bottom: 30rpx;
    opacity: 0.8;
  }
  
  .empty-text {
    font-size: 28rpx;
    color: #999;
    margin-bottom: 40rpx;
    background: linear-gradient(135deg, #999, #666);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
  }
  
  .go-shopping-btn {
    background: linear-gradient(135deg, #ff9a9e, #ff6b6b);
    color: #fff;
    font-size: 28rpx;
    padding: 20rpx 60rpx;
    border-radius: 40rpx;
    border: none;
    box-shadow: 0 8rpx 32rpx rgba(255, 107, 107, 0.2);
    transition: all 0.3s ease;
    
    &:active {
      transform: scale(0.95);
      box-shadow: 0 4rpx 16rpx rgba(255, 107, 107, 0.15);
    }
  }
}
</style>